package db

import (
	log2 "github.com/name5566/leaf/log"
	"go.mongodb.org/mongo-driver/bson"
	"log"
	"math"
	"server/cfg"
	"server/db"
)

const CARDTABLENAME = "t_card"

type CardInfo struct {
	CardId     string          `bson:"card_id"`
	UserId     string          `bson:"user_id"`
	Star       int64           `bson:"star"`
	Level      int64           `bson:"level"`
	SkillList  map[int32]int64 `bson:"skill_list"`
	WorkList   map[int32]int64 `bson:"work_list"`
	FateList   map[int32]int64 `bson:"fate_list"`
	SkinSkill  map[int32]int64 `bson:"skin_skill"`
	HolaList   map[int32]int64 `bson:"hola_list"`
	UniqueList map[int32]int64 `bson:"unique_list"`
	CTId       int64           `bson:"ct_id"`
	Ability    float64         `json:"ability"`
	IsArtist   bool            `json:"is_artist"`
	// 假设魅力和能力值可以通过 Talent 计算
	Talent int64 `bson:"talent"`
	// 新增魅力和能力值字段
	Charm        int64 `bson:"charm"`
	IsZaiGongZuo bool  `bson:"is_zai_gong_zuo" json:"is_zai_gong_zuo"`
}

// 从数据库中获取所有卡牌信息魅力值
func calculateCharm(card CardInfo) int64 {
	return card.Star * 10
}

// 计算卡牌的能力值，这里简单示例为等级 * 5
func calculateAbility(card CardInfo) float64 {
	return getTianFun(card)
}
func getJiChuNengLi(card CardInfo) float64 {
	config := cfg.ConfigData.TbCardBase.Get(int32(card.CTId))
	return (float64(config.Talent) + jiNengTianFu(card) + fuYeTianFu(card) + fuZhuangTianFu(card) + guangHuanTianFu(card)) * levelXiShu(card)

}
func getTianFun(card CardInfo) float64 {
	return getJiChuNengLi(card)*(1+geZhongtuJinJiaCheng()) + geZhongTuJinZengZhi()
}
func geZhongtuJinJiaCheng() float64 {
	return 0
}
func geZhongTuJinZengZhi() float64 {
	return 0
}
func fuZhuangTianFu(card CardInfo) float64 {
	return 0
}
func guangHuanTianFu(card CardInfo) float64 {
	return 0
}
func jiNengTianFu(card CardInfo) float64 {
	return 0
}
func fuYeTianFu(card CardInfo) float64 {
	return 0
}
func levelXiShu(card CardInfo) float64 {
	levelSquared := math.Pow(float64(card.Level), 2)
	// 计算 1.51 * (10^-6) * (card.Level^2)
	term1 := 1.51 * math.Pow(10, -6) * levelSquared
	// 计算 (2.17 * 10^-4) * card.Level
	term2 := 2.17 * math.Pow(10, -4) * float64(card.Level)
	// 计算 0.0316
	term3 := 0.0316
	// 计算括号内的和
	sum := term1 + term2 + term3
	// 最终结果
	result := 10000 * sum
	return result
}

// 从数据库读取玩家的卡牌数据并计算魅力和能力值
func CalculateCardStats(filter interface{}) error {
	// 从数据库中读取所有卡牌数据
	//filter := bson.M{} // 这里可以根据需要添加过滤条件，例如按用户 ID 过滤
	var cards []CardInfo
	err := db.Db.Find(CARDTABLENAME, filter, &cards)
	if err != nil {
		log.Printf("从数据库读取卡牌数据失败: %v", err)
		return err
	}

	// 遍历每个卡牌，计算魅力和能力值
	for _, card := range cards {
		charm := calculateCharm(card)
		ability := calculateAbility(card)
		// 构建更新的字段
		update := bson.M{
			"$set": bson.M{
				"charm":   charm,
				"ability": ability,
			},
		}

		// 定义筛选条件，根据卡牌 ID 进行更新
		selector := bson.M{"card_id": card.CardId}

		// 执行更新操作
		err := db.Db.Update(CARDTABLENAME, selector, update)
		if err != nil {
			log2.Error("更新卡牌数据失败: 卡牌 ID %s, 错误: %v", card.CardId, err)
		} else {
			log2.Debug("卡牌 ID: %s, 魅力值: %d, 能力值: %d 已更新\n", card.CardId, charm, ability)

		}
	}
	return nil
}
